package com.huawei.hbase.tables;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;

import java.io.IOException;

/**
 * 创建Hbase表
 */

public class CreateTable {

    public static void main(String[] args) throws IOException {

        //链接hbase
        Configuration conf = HBaseConfiguration.create();
        //这里的zookeeper地址要改为自己集群的zookeeper地址
        conf.set("hbase.zookeeper.quorum",
                "xxx.xxx.xxx.xxx,xxx.xxx.xxx.xxx,xxx.xxx.xxx.xxx");

        conf.set("hbase.zookeeper.property.clientPort", "2181");

        Connection connection = ConnectionFactory.createConnection(conf);
        Admin admin = connection.getAdmin();

        TableName tableName = TableName.valueOf("users");

        if (!admin.tableExists(tableName)){
            //创建表描述器
            HTableDescriptor htd = new HTableDescriptor(tableName);
            htd.addFamily(new HColumnDescriptor("f"));
            admin.createTable(htd);
            System.out.println(tableName+"表创建成功");
        }else {
            System.out.println(tableName+"表已经存在");
        }
    }
}